package com.example.springboot.config;

import com.example.springboot.component.LoginHandleInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/*
* 调用WebMvcConfigurer接口去扩展springmvc 也就是自己的配置和自动配置是同时起作用的
* */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/main").setViewName("board");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //对所有/下面的访问都拦截 除了登录相关的访问
        registry.addInterceptor(new LoginHandleInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/", "/login.html", "/user/login");
    }
}
